var ctx_val = $("#context_val").val();
var deploy = function(){
	/**
	var iframeName = new Date().getTime();
	window.parent.addTabInSon("工单分派",iframeName)
	$("#deployForm").attr("target",iframeName);
	$("#deployForm").submit();
	**/
	
	
	var billStt = $('#billStt').combobox('getValue');
	
	if("0"==billStt){
		//话务员分派
		BindProvinceCity();
		$("#assign_area_dialog").dialog('open');
		
	}else if("6"==billStt){
		//区域主管分派
		$("#assign_manager_dialog").dialog('open');
		getManagedEmp();
		
		var account = $("#account").val();
		$("#manager_id").val(account);
	}else{
		var billStr = $('#billStt').combobox('getText');
		$.messager.alert('系统提示', "当前工单处于【"+billStr+"】状态，无法进行分派！", 'warning');
	}
	
};

var cancle = function(){
	$.messager.confirm('确认', '确认作废工单吗？', function(r){
		if (r){
			$.ajax({
				type: "POST",
				url:ctx_val+'/workBill/cancleWorkBill?id='+$("#billId").val(),
				dataType:"json",
				success: function(json){
					$.messager.show({
						title:'成功',
						msg:'作废成功',
						showType:'show'
					});
					location.reload();
				}
			});
		}
	});
}
$(function(){
	$("#deploy").click(deploy);
	$("#cancle").click(cancle);
	
	$("#assign_area_dialog").dialog('close');
	$("#assign_manager_dialog").dialog('close');
});

//区域联动
function BindProvinceCity() {
	$.ajax({
		type: "POST",
		url:ctx_val+'/workBill/selectAreaInfo',
		dataType:"json",
		success: function(json){
		$("#assign_area").combobox({
			data:json.rows,
			valueField:'id',
			textField:'areaName',
			onChange: function (newValue, oldValue) {
	            $.get(ctx_val+'/workBill/selectEmpInfo', { areaId: newValue }, function (data) {
	            	if(data.rows.length ==0){
	            		$.messager.show({
							title:'错误',
							msg:'您选择的区域，没有找到对应的主管员工信息，无法进行分派',
							showType:'show'
					   });
	            		area_emp.combobox("clear");
	            	}else{
	            		area_emp.combobox("clear").combobox('loadData', data.rows);
	            	}
	            }, 'json');
	        }
		});
		}
	});
    var area_emp = $('#area_emp').combobox({
        valueField: 'id', //值字段
        textField: 'account', //显示的字段
        editable: true,
        onChange: function (newValue, oldValue) {
        	
        }
    });
}

function area_AssignHander(){
	
	var roleId = $('#area_emp').combobox('getValue');
	if(null==roleId || roleId.length==0){
		window.alert("请选择主管员工！");
		return false;
	}
	var billId =$("#workBillId").val();
	
	 var _json = jQuery.param({ "billId": billId, "roleId": roleId });  
	 $("#assign_area_dialog").dialog("close");
	  $.ajax({  
	        url: ctx_val + "/workBill/workBillAreaAssign",  
	        type: "POST",  
	        async: false,  
	        data: _json, //不能直接写成 {id:"123",code:"tomcat"}  
	        dataType: "json",  
	        // contentType: "charset=utf-8",  
	        cache: false,  
	        success: function (r, textStatus) {  
	            var result = r.result;
	            if("OK"==result){
	            	$("#deployForm").attr("method", "post" );
					$("#deployForm").attr("action",
							ctx_val + "/workBill/viewBillById?id=" + billId);
					$("#deployForm").submit();
	            }else{
	            	$.messager.alert('系统提示', "操作失败，请稍后再试！", 'warning');
	            }
	        },  
	        error: function (XMLHttpRequest, textStatus, errorThrown) { alert(XMLHttpRequest.readyState); }  
	    });  
}

function getManagedEmp(){
	
	var empId = $("#empId").val();
	 var _json = jQuery.param({ "parentId": empId });  
	$.ajax({
		type: "POST",
		data: _json, 
		url:ctx_val+'/workBill/getManagedEmp',
		dataType:"json",
		success: function(json){
			$("#managed_emp").combobox({
				data:json.rows,
				valueField:'id',
				textField:'empName',
			});

		}

	});
}
/*主管分派*/
function manager_AssignHander(){
	var empId = $('#managed_emp').combobox('getValue');
	
	var workBillId = $("#billId").val();
	var _json = jQuery.param({ "empId": empId ,"workBillId":workBillId});  
	 $("#assign_manager_dialog").dialog("close");
	  $.ajax({  
	        url: ctx_val + "/workBill/managedEmpAssignByView",  
	        type: "POST",  
	        async: false,  
	        data: _json, //不能直接写成 {id:"123",code:"tomcat"}  
	        dataType: "json",  
	        // contentType: "charset=utf-8",  
	        cache: false,  
	        success: function (r, textStatus) {  
	            var result = r.result;
	            if("OK"==result){
	            	$("#deployForm").attr("method", "post" );
					$("#deployForm").attr("action",
							ctx_val + "/workBill/viewBillById?id=" + workBillId);
					$("#deployForm").submit();
	            }else{
	            	$.messager.alert('系统提示', "操作失败，请稍后再试！", 'warning');
	            }
	        },  
	        error: function (XMLHttpRequest, textStatus, errorThrown) { alert(XMLHttpRequest.readyState); }  
	    });  
	
}